package com.cmwa.solution.sys.menu.tag;

import java.io.IOException;
import java.io.StringWriter;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import com.cmwa.solution.base.controller.IndexController;
import com.cmwa.solution.sys.client.Client;
import com.cmwa.solution.sys.client.ClientManager;
import com.cmwa.solution.sys.menu.entity.MenuVo;
import com.cmwa.solution.sys.menu.service.MenuService;
import com.cmwa.solution.utils.spring.ContextHolderUtil;
import com.cmwa.solution.utils.spring.SpringContextUtil;
import com.cmwa.solution.utils.velocity.VelocityEngineTool;

public class OperationButtonTag extends TagSupport {
	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(IndexController.class);
	
	/** 操作按钮ID */
	protected String id;
	protected String type = "0";
	protected String fun = "";
	protected String param = "";
	
	protected String role_type = "";
	protected String authority_resource_id = "";
	

	public int doStartTag() throws JspException {
		return super.doStartTag();
	}

	public int doEndTag() throws JspException {

		try {
			JspWriter out = this.pageContext.getOut();
			out.print(end());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return EVAL_PAGE;

	}

	protected String end() {
		MenuService menuService = SpringContextUtil.getBean(MenuService.class);
		MenuVo menu = menuService.queryMenuByCode(id);
		HttpSession session = ContextHolderUtil.getSession();
		Client client = ClientManager.getInstance().getClient(session.getId());
		String html = "";
		if (client != null && menu != null && client.getButtonMenus() != null) {
			if (client.getButtonMenus().containsKey(id)) {
				// 如果层级和资源ID不为空，则根据人+资源+操作判断是否有该按钮的操作权限
				if (StringUtils.isNotBlank(role_type) && StringUtils.isNotBlank(authority_resource_id)) {
					int isHaing = 0;
					if (isHaing > 0) {
						html = getButtonHtml(menu);
					}
				} else {
					html = getButtonHtml(menu);
				}
			}
		}
		return html;
	}

	public String getButtonHtml(MenuVo menu) {
		try {
			log.error("------获取按钮具体信息----------");
			log.error(menu.toString());
			VelocityEngine velocityEngine = SpringContextUtil.getBean(VelocityEngineTool.class).getVelocityEngine();
			log.error("------OPERA_BUTTON.vm---------");
			Template t = velocityEngine.getTemplate("menu/template/OPERA_BUTTON.vm", "UTF-8");
			// 设置初始化数据
			VelocityContext context = new VelocityContext();
			context.put("id", id);
			context.put("type", type);
			context.put("menu", menu);
			//兼容IE浏览器语法检查
			if(StringUtils.isBlank(fun)){
				fun = "void(0)";
			}
			context.put("fun", fun);
			context.put("param", param);
			// 设置输出
			StringWriter writer = new StringWriter();
			// 将环境数据转化输出
			t.merge(context, writer);
			return writer.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getParam() {
		return param;
	}

	public void setParam(String param) {
		this.param = param;
	}

	public String getFun() {
		return fun;
	}

	public void setFun(String fun) {
		this.fun = fun;
	}

	public String getRole_type() {
		return role_type;
	}

	public void setRole_type(String role_type) {
		this.role_type = role_type;
	}

	public String getAuthority_resource_id() {
		return authority_resource_id;
	}

	public void setAuthority_resource_id(String authority_resource_id) {
		this.authority_resource_id = authority_resource_id;
	}
}